package com.zlm.user.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.PosititonApplyConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.CoordinatesDto;
import com.zlm.common.dto.HxUserDto;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.EmpInfoParamsDto;
import com.zlm.common.dto.userEmp.EmpInfoResponseDto;
import com.zlm.common.enums.IdNoStatusEnum;
import com.zlm.common.util.MD5Utils;
import com.zlm.user.emp.entity.SysEmpDO;
import com.zlm.common.dto.userEmp.SysEmpDto;
import com.zlm.user.emp.service.EmpPositionApplyService;
import com.zlm.user.emp.service.EmpPositionSignService;
import com.zlm.user.emp.service.UserRelationService;
import com.zlm.user.emp.service.SysEmpService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户controller-（内部服务）
 * @Author: duyufu
 * @Date: 2019/4/19 11:30
 */
@RestController
@RequestMapping("/emp")
public class SysEmpController extends BaseController {

    @Autowired
    private SysEmpService sysEmpService;
    @Autowired
    private EmpPositionSignService empPositionSignService;
    @Autowired
    private EmpPositionApplyService empPositionApplyService;
    @Autowired
    private UserRelationService userRelationService;

    /**
     * 查询用户
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping(value = "/queryEmpByMobileAndPassword")
    public JsonResultDto queryEmpByMobileAndPassword(String mobile, String password) {
        try {
            SysEmpDto empDto = sysEmpService.queryEmpByMobileAndPassword(mobile, password);
            if (null == empDto) {
                return fail(CodeConstants.CODE_10004);
            }
            return success(empDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 是否存在该手机号
     * @param mobile
     * @return
     */
    @GetMapping("/isExistMobile")
    public JsonResultDto isExistMobile(String mobile) {
        boolean b;
        try {
            b = sysEmpService.isExistMobile(mobile);
            return success(b);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 根据参数获取用户
     * @param emp
     * @return
     */
    @PostMapping("/queryEmp")
    public JsonResultDto queryEmp(@RequestBody SysEmpDO emp) {
        try {
            SysEmpDto empDto = sysEmpService.queryEmp(emp);
            return success(empDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 修改密码
     * @param mobile
     * @param password
     * @return
     */
    @GetMapping("/updatePassword")
    public JsonResultDto updatePassword(String mobile, String password) {
        try {
            sysEmpService.updatePassword(mobile, password);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    @GetMapping("/updatePasswordByToken")
    public JsonResultDto updatePasswordByToken(Integer empId,String oldPassword,String newPassword) {
        try {
            String password = sysEmpService.queryEmpPassword(empId);
            oldPassword = MD5Utils.getStringMD5(oldPassword);
            if (StringUtils.isBlank(password) || !password.equals(oldPassword)) {
                return fail(CodeConstants.CODE_10004);
            }
            newPassword = MD5Utils.getStringMD5(newPassword);
            sysEmpService.updatePasswordByEmpId(empId,newPassword);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }

    }

    @GetMapping("/getEmpInfo")
    public JsonResultDto getEmpInfo(Integer empId) {
        try {
            //查询用户基本信息
            SysEmpDto emp = sysEmpService.queryEmpById(empId);
            //查看用户自己报名总数量
            Integer empPostCount = empPositionSignService.countEmpPositionSign(null,empId);
            //查看合伙人报名总数量
            Integer partnerPostCount = empPositionApplyService.countPartnerPost(empId, null);
            //查看待面试的数量
            Integer waitInterviewCount = empPositionApplyService.countPartnerPost(empId, PosititonApplyConstants.APPLY_102);
            //查看已入职的数量
            Integer entryCount = empPositionApplyService.countPartnerPost(empId,PosititonApplyConstants.APPLY_113) +
                    empPositionApplyService.countPartnerPost(empId,PosititonApplyConstants.APPLY_106);
            //封装参数
            EmpInfoResponseDto empInfo = new EmpInfoResponseDto();
            empInfo.setAvatar(emp.getAvatar());
            empInfo.setSex(emp.getSex());
            empInfo.setEmpName(StringUtils.isBlank(emp.getEmpName()) ? UserConstants.USER_DEFAULT_NAME :emp.getEmpName());
            empInfo.setIdNoStatus(emp.getIdNoStatus());
            empInfo.setIdNoStatusName(IdNoStatusEnum.getNameByCode(emp.getIdNoStatus()));
            empInfo.setProvince(emp.getProvince());
            empInfo.setCity(emp.getCity());
            empInfo.setArea(emp.getArea());
            empInfo.setIsPerfect(emp.getIsPerfect());
            empInfo.setEmpPostCount(empPostCount);
            empInfo.setPartnerPostCount(partnerPostCount);
            empInfo.setWaitInterviewCount(waitInterviewCount);
            empInfo.setEntryCount(entryCount);
            return success(empInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 保存用户信息
     * @param empInfoParamsDto
     * @return
     */
    @PostMapping("/saveEmpInfo")
    public JsonResultDto saveEmpInfo(@RequestBody EmpInfoParamsDto empInfoParamsDto) {
        try {
            sysEmpService.saveEmpInfo(empInfoParamsDto);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 实名认证
     * @param empInfo
     * @return
     */
    @PostMapping("/realNameAuthentication")
    public JsonResultDto realNameAuthentication(@RequestBody Map<String,Object> empInfo) {
        try {
            String idNo = (String)empInfo.get("idNo");
            int count = sysEmpService.countEmpByIdNo(idNo);
            if (count > 0) {
                throw new RuntimeException(CodeConstants.CODE_10014);
            }
            sysEmpService.realNameAuthentication(empInfo);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加好友
     * @param userHxId
     * @param friendHxId
     * @return
     */
    @GetMapping("/addFriend")
    public JsonResultDto addFriend(String userHxId,String friendHxId) {
        try {
            isExistedHxUser(userHxId);
            isExistedHxUser(friendHxId);
            userRelationService.addFriend(userHxId,friendHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除好友
     * @param userHxId
     * @param friendHxId
     * @return
     */
    @GetMapping("/deleteFriend")
    public JsonResultDto deleteFriend(String userHxId,String friendHxId) {
        try {
            isExistedHxUser(userHxId);
            isExistedHxUser(friendHxId);
            userRelationService.deleteFriend(userHxId,friendHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    private SysEmpDto isExistedUser(Integer ownerUsername) {
        SysEmpDto empDto = sysEmpService.queryEmpById(ownerUsername);
        if (null == empDto) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }else if (StringUtils.isBlank(empDto.getHxId())) {
            throw new RuntimeException(CodeConstants.CODE_10019);
        }
        return empDto;
    }

    private HxUserDto isExistedHxUser(String hxId) {
        HxUserDto userDto = sysEmpService.queryHxUser(hxId);
        if (null == userDto) {
            throw new RuntimeException(CodeConstants.CODE_103);
        }
        return userDto;
    }

    /**
     * 查询好友列表
     * @param userHxId
     * @return
     */
    @GetMapping("/queryFriendList")
    public JsonResultDto queryFriendList(String userHxId) {
        try {
            isExistedHxUser(userHxId);
            List<HxUserDto> friendList = userRelationService.queryFriendList(userHxId);
            return success(friendList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取单个用户
     * @param userHxId
     * @return
     */
    @GetMapping("/queryHxUser")
    public JsonResultDto queryHxUser(String userHxId) {
        try {
            HxUserDto userDto = sysEmpService.queryHxUser(userHxId);
            return success(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 搜索好友
     * @param keyword
     * @return
     */
    @GetMapping("/searchFriend")
    public JsonResultDto searchFriend(String keyword) {
        try {
            List<HxUserDto> friendList = sysEmpService.searchFriend(keyword);
            return success(friendList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }

    }

    /**
     * 附近人才
     * @param pageNumber
     * @param pageSize
     * @param lng
     * @param lat
     * @return
     */
    @GetMapping("/queryLocalEmp")
    public JsonResultDto queryLocalEmp(Integer pageNumber,Integer pageSize, Double lng, Double lat) {
        try {
            CoordinatesDto coordinates = new CoordinatesDto(lng, lat);
            if (!coordinates.isValid()) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            PageEntity page = sysEmpService.queryLocalEmp(pageNumber,pageSize,coordinates);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

}
